<template>
  <div class="login-container">
    <el-form :model="form" @submit.prevent="login" class="login-form">
      <h2 class="login-title">用户登录</h2>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" />
      </el-form-item>
      <el-button type="primary" native-type="submit" class="login-btn">登录</el-button>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import api from '../api'
import { ElMessage } from 'element-plus'

const form = ref({ password: '' })
const router = useRouter()

const login = async () => {
  try {
    const res = await api.login(form.value.password)
    if (res.data.status === 1) {
      sessionStorage.setItem('isLogin', 'true')
      router.push('/home')
    } else {
      ElMessage.error('密码错误')
    }
  } catch (error) {
    ElMessage.error('登录失败')
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f0f2f5;
}

.login-form {
  width: 300px;
  padding: 40px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  margin-top: -8rem;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.login-btn {
  width: 100%;
  margin-top: 20px;
}

/* 调整表单项间距 */
.el-form-item {
  margin-bottom: 28px;
}

/* 输入框宽度调整 */
.el-input {
  width: 90%;
}
</style>